package de.lulusoft.anothertorrent.gui.views;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Item;
import org.eclipse.ui.part.ViewPart;

import de.lulusoft.anothertorrent.core.rtorrent.TorrentCommand;
import de.lulusoft.anothertorrent.core.rtorrent.TorrentCommands;
import de.lulusoft.anothertorrent.gui.views.Providers.TableColumnSorter;
import de.lulusoft.anothertorrent.gui.views.Providers.TorrentCommandsViewContentProvider;
import de.lulusoft.anothertorrent.gui.views.Providers.TorrentCommandsViewLabelProvider;

public class TorrentCommandsView extends ViewPart {
	
	public static final String ID = "de.lulusoft.anothertorrent.gui.views.TorrentCommandsView";
	
	private static final String columnNames[] = {"Name", "Actions"};
	private TableViewer viewer = null;

	
	
	public TorrentCommandsView() {
	}

	@Override
	public void createPartControl(Composite parent) {
		
		viewer = new TableViewer(parent, SWT.FULL_SELECTION);
		viewer.setColumnProperties(columnNames);
		viewer.getTable().setHeaderVisible(true);
		viewer.getTable().setLinesVisible(true);
		
		// add columns
		int i = 0;
		for (final String prop : columnNames)
		{
			final int colIdx = i;
			TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
			column.getColumn().setText(prop);
			column.getColumn().setWidth(100);
			column.getColumn().setMoveable(true);
			
			// Add Sorters..
			TableColumnSorter cSorter = new TableColumnSorter(viewer, column.getColumn()) 
			{
			     protected int doCompare(Viewer v, Object e1, Object e2) 
			     {
			    	 ITableLabelProvider lp = ((ITableLabelProvider) viewer
			    			 .getLabelProvider());
		             String s1 = lp.getColumnText(e1, colIdx);
		             String s2 = lp.getColumnText(e2, colIdx);
		             return s1.compareTo(s2);
			     }
			};

			cSorter.setSorter(cSorter, TableColumnSorter.ASC);
			i++;
		}
		
		// Create the cell editors
	    CellEditor[] editors = new CellEditor[columnNames.length];
	    TextCellEditor textEditor = new TextCellEditor(viewer.getTable());
        editors[0] = textEditor;
        editors[1] = textEditor;
        
        // Assign the cell editors to the viewer 
        viewer.setCellEditors(editors);
        
        viewer.setCellModifier(new ICellModifier() {
		
			@Override
			public void modify(Object element, String property, Object value) {
				
				if (element instanceof Item) {
			        element = ((Item) element).getData();
			    }
			    // modify the element's property here
				if (element instanceof TorrentCommand)
				{
					TorrentCommand tc = (TorrentCommand)element;
					if (property.equals("Name"))
					{
						tc.setName(value.toString());
						TorrentCommands.getINSTANCE().save();
						viewer.update(tc, null);
					}
					else if (property.equals("Actions"))
					{
						tc.setCommand(value.toString());
						TorrentCommands.getINSTANCE().save();
						viewer.update(tc, null);
					}
				}
		
			}
		
			@Override
			public Object getValue(Object element, String property) {
				if (element instanceof TorrentCommand)
				{
					TorrentCommand tc = (TorrentCommand)element;
					if (property.equals("Name"))
					{
						return tc.getName();
					}
					else if (property.equals("Actions"))
					{
						return tc.getCommand();
					}
				}
				return null;
			}
		
			@Override
			public boolean canModify(Object element, String property) {
				if (element instanceof TorrentCommand)
				{
					if (property.equals("Name"))
						return true;
					else if (property.equals("Actions"))
						return true;
				}
				return false;
			}
		});
        
        ColumnViewerEditorActivationStrategy strategy = new ColumnViewerEditorActivationStrategy(viewer);
        strategy.setEnableEditorActivationWithKeyboard(true);
		        
		viewer.getTable().addMouseListener(new MouseListener() {
			@Override
			public void mouseUp(MouseEvent e) {}
			@Override
			public void mouseDown(MouseEvent e) {}
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				TorrentCommand tc = TorrentCommands.getINSTANCE().addCommand("Hello World", "d.set_custom1=Hello,d.set_custom2=World");
					viewer.add(tc);
				}
		});
   
		viewer.getTable().addKeyListener(new KeyListener() {
			@Override
			public void keyReleased(KeyEvent e) {
				if (e.keyCode == SWT.DEL)
				{
					if (((IStructuredSelection)viewer.getSelection()).getFirstElement() instanceof TorrentCommand)
					{
						TorrentCommand tc = (TorrentCommand) ((IStructuredSelection)viewer.getSelection()).getFirstElement();
						viewer.remove(tc);
						TorrentCommands.getINSTANCE().getCommands().remove(tc);
						TorrentCommands.getINSTANCE().save();
					}
				}
			}
		
			@Override
			public void keyPressed(KeyEvent e) {}
		});
		
        viewer.setContentProvider(new TorrentCommandsViewContentProvider());
        viewer.setLabelProvider(new TorrentCommandsViewLabelProvider());
        
        viewer.setInput(TorrentCommands.getINSTANCE());


	}

	@Override
	public void setFocus() {
		viewer.getTable().setFocus();
	}

}
